import javax.swing.*;

public class IndexPage {
    JFrame frame;
    JPanel currentPanel;
    JMenuBar currentMenuBar;
    String noticePage;
    //词法分析页
    LexicalAnalysisPage lexicalAnalysisPage=new LexicalAnalysisPage();
    //正规文法页
    NormalGrammarPage normalGrammarPage=new NormalGrammarPage();
    //语法分析页
    LookingAnalysisPage lookingAnalysisPage=new LookingAnalysisPage();
    //额外切换菜单按钮
    JMenu menu_switch;
    JMenuItem lexicalAnalysisMenuItem;
    JMenuItem NormalGrammarMenuItem;
    JMenuItem LookingAnalysisMenuItem;


    IndexPage(){
        frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setTitle("基本编译器-20250902");
        // 设置窗口大小
        frame.setSize(1280, 720);
        // 将窗口显示屏幕中央
        frame.setLocationRelativeTo(null);
        frame.setLayout(null);
        //不允许改变大小
        frame.setResizable(false);

        //菜单切换
        menu_switch = new JMenu("切换功能页面");
        lexicalAnalysisMenuItem = new JMenuItem("词法分析页面");
        NormalGrammarMenuItem = new JMenuItem("正规文法页面");
        LookingAnalysisMenuItem = new JMenuItem("预测分析页面");
        menu_switch.add(NormalGrammarMenuItem);
        menu_switch.add(lexicalAnalysisMenuItem);
        menu_switch.add(LookingAnalysisMenuItem);

        //初始词法分析
        currentPanel=new JPanel();
        this.ToLexicalAnalysis();
        this.Fresh();

        //菜单功能
        lexicalAnalysisMenuItem.addActionListener(e -> {
            if (noticePage.equals("词法分析页面")){
                JOptionPane.showMessageDialog(frame, "当前页面已处于词法分析页面！");
            }
            else{
                ToLexicalAnalysis();
                this.Fresh();
            }
        });
        NormalGrammarMenuItem.addActionListener(e -> {
            if (noticePage.equals("正规文法页面")){
                JOptionPane.showMessageDialog(frame, "当前页面已处于正规文法页面！");
            }
            else{
                ToNormalGrammar();
                this.Fresh();
            }
        });
        LookingAnalysisMenuItem.addActionListener(e -> {
            if (noticePage.equals("预测分析页面")){
                JOptionPane.showMessageDialog(frame, "当前页面已处于预测分析页面！");
            }
            else{
                ToLookingAnalysis();
                this.Fresh();
            }
        });

        // 在所有组件添加完毕后再设置可见性
        frame.setJMenuBar(currentMenuBar);
        frame.add(currentPanel);
        frame.setVisible(true);
    }

    //重载
    private void Fresh(){
        // 先移除旧的面板和菜单栏
        frame.remove(currentPanel);
        frame.setJMenuBar(null);
        currentMenuBar.add(menu_switch);
        frame.setJMenuBar(currentMenuBar);
        frame.add(currentPanel);

        // 重新验证和重绘
        frame.revalidate();
        frame.repaint();
    }
    private void ToLexicalAnalysis(){
        frame.remove(currentPanel);
        System.out.println("点击了词法分析");
        currentPanel = lexicalAnalysisPage.SwitchLexicalAnalysis();
        currentMenuBar = lexicalAnalysisPage.GetLexicalAnalysisMenuBar();
        noticePage="词法分析页面";
        frame.add(currentPanel);
    }
    private void ToNormalGrammar(){
        frame.remove(currentPanel);
        System.out.println("点击了正规文法");
        currentPanel = normalGrammarPage.SwitchNormalGrammar();
        currentMenuBar = normalGrammarPage.GetNormalGrammarMenuBar();
        noticePage="正规文法页面";
        frame.add(currentPanel);
    }
    private void ToLookingAnalysis(){
        frame.remove(currentPanel);
        System.out.println("点击了预测分析");
        currentPanel = lookingAnalysisPage.SwitchLookingAnalysis();
        currentMenuBar = lookingAnalysisPage.GetLookingAnalysisMenuBar();
        noticePage="预测分析页面";
        frame.add(currentPanel);
    }

}
